package com.htfg.cardActivate.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @Auther: 王晨缘
 * @Date: 2018/10/24 0024 17:11
 * @Description:
 */
public class DateUtil {
    private static final ThreadLocal<DateFormat> messageFormat = new ThreadLocal<DateFormat>();
    private static final ThreadLocal<DateFormat> messageFormat2 = new ThreadLocal<DateFormat>();
    private static final String MESSAGE_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final String MESSAGE_FORMAT2 = "yyyy-MM-dd";
    /**
     * 时间优化避免线程不安全
     *
     * @return 返货DateFormat
     */
    public static final DateFormat getDateFormat() {
        DateFormat format = messageFormat.get();
        if (format == null) {
            format = new SimpleDateFormat(MESSAGE_FORMAT, Locale.getDefault());
            messageFormat.set(format);
        }

        return format;
    } /**
     * 时间优化避免线程不安全
     *
     * @return 返货DateFormat
     */
    public static final DateFormat getDateFormat2() {
        DateFormat format = messageFormat2.get();
        if (format == null) {
            format = new SimpleDateFormat(MESSAGE_FORMAT2, Locale.getDefault());
            messageFormat2.set(format);
        }

        return format;
    }
    /**
     * 获取当前的时间 yyyy-MM-dd HH:mm:SS
     * @return
     */
    public static Date getNewDateYmdHms(){
        return new Date();
    }

    /**
     * 转换为date
     * @param date
     * @return
     */
    public  static Date getDateByYmdHms(String date){
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse=null;
        try {
             parse = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return parse;
    }
    /**
     * java计算两个日期相差多少天小时分钟等
     * @param endDate
     * @param nowDate
     * @return
     */
    public static String getDatePoor(Date endDate, Date nowDate) {

        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    public static void main(String[] args) throws ParseException {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date parse = sdf.parse("2018-09-01 00:00:00");
        String datePoor = getDatePoor(parse,new Date());
        System.out.println(datePoor);
    }
}
